/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.action.search;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.grayrabbit.cms.entity.KeywordsRecommend;
import org.grayrabbit.cms.service.search.KeywordsRecommendService;
import org.grayrabbit.cms.vo.search.KeywordsRecommendBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import org.grayrabbit.dao.util.Page;
import org.grayrabbit.web.struts2.GenericAction;

/**
 * <b>KeywordsRecommendAction</b> Struts2 action�?





 * @version 1.0,创建时间:2011-10-26 13:42:14 
 */
@Controller
@Scope("prototype")
public class KeywordsRecommendAction extends GenericAction<KeywordsRecommendBean> {
	private final static Logger log = LoggerFactory.getLogger(KeywordsRecommendAction.class);
	private static final long serialVersionUID = -1L;
	//Service接口接入
	@Autowired
	@Qualifier("keywordsRecommendServiceImpl")
	private KeywordsRecommendService keywordsRecommendService;
		
	//VO对象，由前端页面数据组装
	private KeywordsRecommendBean keywordsRecommend;
	
	
	private String[] kwrecommendId;
	private String[] recommendName;
	private String[] recommendurl;
	private Integer[] recommendtype;
	private String[] introduction;
	
	/**return 前端页面数据组装VO对象,<b>KeywordsRecommendBean</b>*/
	public KeywordsRecommendBean getKeywordsRecommend() {
		return keywordsRecommend;
	}

	public void setKeywordsRecommend(KeywordsRecommendBean keywordsRecommend) {
		this.keywordsRecommend = keywordsRecommend;
	}
	
	//分页对象
	private Page<KeywordsRecommend> page = new Page<KeywordsRecommend>(MAX_PAGE_NUM,true);
	
	/*
	 * return分页对象，默认实例化。如：page.pageNu=2,将更新相应的对象property
	 */
	public Page<KeywordsRecommend> getPage() {
		return page;
	}
	//接收前端form,url提交参数组装成分页property
	public void setPage(Page<KeywordsRecommend> page) {
		this.page = page;
	}
	
	/**
	 * 在执行（struts2的action调用时）list()方法前执行本方法
	 */
	public void prepareList(){
		prepareModel();
	}
	
	/**
	 * 在执行任何其它action方法（struts2的action调用时）前执行本方法
	 */
	public void prepare(){
		if(keywordsRecommend==null){
			keywordsRecommend = new KeywordsRecommendBean();
		}
	}
	/**
	 * 实现ModelDriven的接口简例前端页的bean组装写法,
	 *  如：
	 *	<pre>
	 *		<code> name="user.userName"</code>
	 *	改写成：
	 *		<code> name="userName"</code>
	 *	action实例中，会自动组成user成员变量
	 *	</pre>
	 */
	public KeywordsRecommendBean getModel() {
		return keywordsRecommend;
	}
	
	@Override
	protected void prepareModel(){
		if(keywordsRecommend != null && StringUtils.isNotBlank(keywordsRecommend.getKwrecommendId())){
			keywordsRecommend = (KeywordsRecommendBean)dozer.map(keywordsRecommendService.getKeywordsRecommend(keywordsRecommend.getKwrecommendId()),KeywordsRecommendBean.class);
		}
		
	}

	/**
	 * 删除<b>KeywordsRecommend</b>记录的方法，成员变量<b>key</b>为主键的String数组，以主键方式进行删作
	 * 删除成功后，返回"reload"，然后跳转到keywordsrecommend-list.action
	 */
	@Override
	@Action(value="keywordsrecommend-delete",
			results={@Result(name="reload",type="redirectAction",params={"actionName","keywordsrecommend-list"})}
			)
	public String delete(){
		keywordsRecommendService.delete(key);
		return renderText("删除成功!");
	}
	
	/**
	 * KeywordsRecommend的列表方法，通过成员变量<b>page�?{insClassName}</b>调用service接口，更新page
	 * 执行成功后，将映射到keywordsrecommend-list.jsp页面
	 */
	public String list(){
		return null;
	}
	/**
	 * KeywordsRecommend的新增与编辑的方法执行成功能
	 * 将映射到keywordsrecommend-input.jsp
	 */
	@Action(value="keywordsrecommend-input")
	public String input(){
		//prepareModel();
		page.setPageSize(-1);
		keywordsRecommendService.list(page,keywordsRecommend);
		return SUCCESS;
	}
	
	/**
	 * save or update KeywordsRecommend的方法，将成员property keywordsRecommend转成KeywordsRecommend实例，执行保存；
	 * 执行成功后，返回"reload"，然后跳转到keywordsrecommend-list.action
	 */
	@Override
	@Action(value="keywordsrecommend-save",
			results={@Result(name="reload",type="redirectAction",params={"actionName","keywords-list"})}
		)
	public String save(){
		if(kwrecommendId==null){
			return INPUT;
		}
		for(int i=0;i<kwrecommendId.length;i++){
			try{
				KeywordsRecommend kr = new KeywordsRecommend();
				kr.setKeywords(keywordsRecommend.getKeywords());
				if(StringUtils.isBlank(kwrecommendId[i])){
					kr.setKwrecommendId(null);
				}
				else{
					kr.setKwrecommendId(kwrecommendId[i]);
				}
				kr.setRecommendName(recommendName[i]);
				kr.setRecommendtype(recommendtype[i]);
				kr.setRecommendurl(recommendurl[i]);
				kr.setIntroduction(introduction[i]);
				keywordsRecommendService.save(kr);
			}
			catch (Exception e) {
				e.printStackTrace();
				log.error("推荐链接保存时参数错误",e);
			}
			
		}
		return RELOAD;
	}

	@Action(value="keywordsrecommend-checkurl")
	public String checkurl(){
		JSONObject jo = new JSONObject();
		jo.put("flag", false);
		for(int i=0;i<kwrecommendId.length;i++){
			boolean flag = keywordsRecommendService.isExistUrl(kwrecommendId[i], recommendurl[i]);
			if(flag){
				jo.put("index", i);
				jo.put("flag", flag);
				break;
			}
		}
		return renderText(jo.toString());
	}
	
	public String[] getKwrecommendId() {
		return kwrecommendId;
	}

	public void setKwrecommendId(String[] kwrecommendId) {
		this.kwrecommendId = kwrecommendId;
	}

	public String[] getRecommendName() {
		return recommendName;
	}

	public void setRecommendName(String[] recommendName) {
		this.recommendName = recommendName;
	}

	public String[] getRecommendurl() {
		return recommendurl;
	}

	public void setRecommendurl(String[] recommendurl) {
		this.recommendurl = recommendurl;
	}

	public Integer[] getRecommendtype() {
		return recommendtype;
	}

	public void setRecommendtype(Integer[] recommendtype) {
		this.recommendtype = recommendtype;
	}

	public String[] getIntroduction() {
		return introduction;
	}

	public void setIntroduction(String[] introduction) {
		this.introduction = introduction;
	}
	
}